/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package luft27.util;

/**
 * Implements thread loop and stop logic. 
 * @author amalikov
 */
public abstract class RunnableLoop implements Runnable {

	public RunnableLoop() {
		stop = false;
		thread = null;
	}
	
	/**
	 * Before loop action.
	 */
	public abstract void init();
	
	/**
	 * Loop body.
	 */
	public abstract void cycle();
	
	public void stop() {
		stop = true;
		try {
			thread.join();
		} catch (InterruptedException ex) {}
	}
	
	@Override
	public void run() {
		thread = Thread.currentThread();
		init();
		while (!stop)
			cycle();
	}
	
	private boolean stop;
	private Thread thread;
}
